---
title: "‎"
categories:
- devops
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgc5eac4b">Deploy our app :: kubectl run</a>
<ul>
<li><a href="#org0c3cb0e">Deployments</a></li>
<li><a href="#org532b3b1">Example</a></li>
</ul>
</li>
<li><a href="#org47b3483">View our app</a></li>
<li><a href="#org42af144">Using Service to Expose Your App</a>
<ul>
<li><a href="#orgf91d947">kubectl expose</a></li>
<li><a href="#org7c9fff7">type</a></li>
</ul>
</li>
<li><a href="#org64781d8">Scale Your App</a></li>
<li><a href="#org33c1554">Updating Your App</a>
<ul>
<li><a href="#org8d54461">Update</a></li>
<li><a href="#orge6d3bff">Rollout</a></li>
</ul>
</li>
<li><a href="#orgdae2add">Pod Operation</a>
<ul>
<li><a href="#orgc934f03">Get Pods :: kubectl get pods</a></li>
<li><a href="#org168c651">Show Pods :: kubectl describe pods</a></li>
<li><a href="#org3e56fe9">Show Pods Log :: kubectl logs $POD_NAME</a></li>
<li><a href="#orgdf32307">在容器中执行命令</a></li>
</ul>
</li>
<li><a href="#orgfba9d28">Service Operations</a>
<ul>
<li><a href="#orge992576">Get Service :: kubectl get services</a></li>
<li><a href="#orgd7adba0">Delete Service</a></li>
</ul>
</li>
<li><a href="#org427f352">Label Operations</a>
<ul>
<li><a href="#orgbbe6933">Get by Label</a></li>
<li><a href="#org4028a9d">Apply New Label</a></li>
</ul>
</li>
<li><a href="#org4dbacbc">Namespaces Operations</a>
<ul>
<li><a href="#org1aac8e7">Get Namespaces :: kubectl get namespaces</a></li>
<li><a href="#org8d0ab64">Create Namespace ::</a></li>
<li><a href="#org4891c9a">Delete Namespace :: kubectl delete namespaces &lt;name&gt;</a></li>
</ul>
</li>
</ul>
</div>
</div>
<p>
The common format of a kubectl command is: kubectl action resource.
</p>
<p>
To view the nodes in the cluster, run the kubectl get nodes command
</p>
<div class="outline-2" id="outline-container-orgc5eac4b">
<h2 id="orgc5eac4b">Deploy our app :: kubectl run</h2>
<div class="outline-text-2" id="text-orgc5eac4b">
<p>
kubectl run NAME –image=image [–env="key=value"] [–port=port] [–replicas=replicas] [–dry-run=bool]
[–overrides=inline-json] [–command] – [COMMAND] [args…] [options]
</p>
</div>
<div class="outline-3" id="outline-container-org0c3cb0e">
<h3 id="org0c3cb0e">Deployments</h3>
<div class="outline-text-3" id="text-org0c3cb0e">
<p>
Kubernates 中的 Deployment, 在图中位于master node中。
Deployment指示Kubernates如何创建和更新应用程序。
一旦创建完成，Kubernate Master将安排应用程序到集群中的结点（运行实例）。
</p>
<p>
当应用程序的实例启动后，Kubernate Deployment Controller一直监控这些实例，
如果运行一个实例的Node挂了或者被删除了，Deployment Controller使用集群中另外一个Node上的实例来替换它。
这种方式提供了自愈的机制。
<object class="org-svg" data="./.kubectl/deployment.svg" type="image/svg+xml">
Sorry, your browser does not support SVG.</object>
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org532b3b1">
<h3 id="org532b3b1">Example</h3>
<div class="outline-text-3" id="text-org532b3b1">
<p>
The {% raw %} run {% endraw %} command creates a new deployment.For example:
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
</pre>
</div>
<p>
这条命令执行如下操作：
</p>
<ol class="org-ol">
<li>查找能启动此程序实例的合适Node</li>
<li>安排程序到这个Node上运行</li>
<li>配置集群，当需要时重新分配应用实例到新的Node上</li>
</ol>
<p>
list your deployment: {% raw %} kubectl get deployments {% endraw %}
</p>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org47b3483">
<h2 id="org47b3483">View our app</h2>
<div class="outline-text-2" id="text-org47b3483">
<p>
kubernates 中运行的pod是位于私有的孤立的网络中。默认情况下，只有同一个kubernates集群中的其他pods和service能访问。
当我们使用kubectl的时候，我们是通过API终结点与我们的应用程序交互的。
</p>
<p>
kubectl能够创建一个代理，与内部的私有网络交互。
</p>
<p>
下面这个命令会阻塞当前shell，所以在新的窗口中打开。通过 ctrl-c 停止，并且运行时不会输出任何信息。
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl proxy
</pre>
</div>
<p>
这样就建立了一个主机到Kubernates集群之间的连接。然后通过下面的命令访问pod：
</p>
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">Deploy nginx and expose 80</span>
kubectl run nginx --image nginx --port 80
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">kubectl proxy # 在另外的shell中运行的命令，建立主机到kubernates集群之间的连接</span>
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">访问nginx</span>
curl http://localhost:8001/api/v1/namespaces/test/pods/nginx-57867cc648-9pqfv/proxy/
<span style="font-weight: bold; font-style: italic;">## </span><span style="font-weight: bold; font-style: italic;">OUTPUT: Nginx服务器欢迎页 html 内容</span>
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org42af144">
<h2 id="org42af144">Using Service to Expose Your App</h2>
<div class="outline-text-2" id="text-org42af144">
<p>
Pod随着Node的消亡而结束其生命周期。
Service是一层抽象，定义pod的逻辑集合，和访问它们的策略。
通过yaml（推荐）或JSON定义Service
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl expose deployment nginx --type=LoadBalancer --port=10080 --target-port=80
</pre>
</div>
</div>
<div class="outline-3" id="outline-container-orgf91d947">
<h3 id="orgf91d947">kubectl expose</h3>
</div>
<div class="outline-3" id="outline-container-org7c9fff7">
<h3 id="org7c9fff7">type</h3>
<div class="outline-text-3" id="text-org7c9fff7">
<dl class="org-dl">
<dt>ClusterIP</dt><dd>default, 自动分配一个仅 cluster 内部可以访问的虚拟 IP</dd>
<dt>NodePort</dt><dd>在ClusterIP的基础上，使用NAT，在集群上的每个Node上使用相同的端口，集群外部能够使用 &lt;NodeIP&gt;:&lt;NodePort&gt;的方式访问服务</dd>
<dt>LoadBalancer</dt><dd>在NodePort的基础上，借助cloud provider（云服务提供商）创建一个LoadBalancer，使用一个固定的外部IP</dd>
<dt>ExternalName</dt><dd>使用spec中指定的externalName作为名字，返回该名字的CNAME记录。没有代理。需要1.7或以上的kube-dns</dd>
</dl>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org64781d8">
<h2 id="org64781d8">Scale Your App</h2>
<div class="outline-text-2" id="text-org64781d8">
<div class="org-src-container">
<pre class="src src-sh">kubectl scale deployments/kubernetes-bootcamp --replicas=4 <span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">扩展成4个，再次 kubectl get pods 能看到有4个ip不同的nginx</span>
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org33c1554">
<h2 id="org33c1554">Updating Your App</h2>
<div class="outline-text-2" id="text-org33c1554">
</div>
<div class="outline-3" id="outline-container-org8d54461">
<h3 id="org8d54461">Update</h3>
<div class="outline-text-3" id="text-org8d54461">
<p>
kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1… CONTAINER_NAME_N=CONTAINER_IMAGE_N [options]
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-orge6d3bff">
<h3 id="orge6d3bff">Rollout</h3>
<div class="outline-text-3" id="text-orge6d3bff">
<p>
kubectl rollout undo deployments/nginx
</p>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-orgdae2add">
<h2 id="orgdae2add">Pod Operation</h2>
<div class="outline-text-2" id="text-orgdae2add">
</div>
<div class="outline-3" id="outline-container-orgc934f03">
<h3 id="orgc934f03">Get Pods :: kubectl get pods</h3>
</div>
<div class="outline-3" id="outline-container-org168c651">
<h3 id="org168c651">Show Pods :: kubectl describe pods</h3>
</div>
<div class="outline-3" id="outline-container-org3e56fe9">
<h3 id="org3e56fe9">Show Pods Log :: kubectl logs $POD_NAME</h3>
<div class="outline-text-3" id="text-org3e56fe9">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold;">export</span> <span style="font-weight: bold; font-style: italic;">POD_NAME</span>=$(<span style="font-weight: bold;">kubectl</span> get pods -o go-template --template <span style="font-style: italic;">{% raw %} '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' {% endraw %}</span>)
<span style="font-weight: bold;">echo</span> Name of the Pod: $<span style="font-weight: bold; font-style: italic;">POD_NAME</span>
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-orgdf32307">
<h3 id="orgdf32307">在容器中执行命令</h3>
<div class="outline-text-3" id="text-orgdf32307">
<p>
当pod运行之后，能够直接在在容器中运行命令
{% raw %} kubectl exec $POD_NAME env {% endraw %}
因为pod中只有一个容器，所以容器的名字能够忽略，例如，运行bash：
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl exec -ti $<span style="font-weight: bold; font-style: italic;">POD_NAME</span> bash
</pre>
</div>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-orgfba9d28">
<h2 id="orgfba9d28">Service Operations</h2>
<div class="outline-text-2" id="text-orgfba9d28">
</div>
<div class="outline-3" id="outline-container-orge992576">
<h3 id="orge992576">Get Service :: kubectl get services</h3>
</div>
<div class="outline-3" id="outline-container-orgd7adba0">
<h3 id="orgd7adba0">Delete Service</h3>
<div class="outline-text-3" id="text-orgd7adba0">
<pre class="example">
kubectl delete service nginx
kubectl delete service -l run=nginx # delete by label
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org427f352">
<h2 id="org427f352">Label Operations</h2>
<div class="outline-text-2" id="text-org427f352">
</div>
<div class="outline-3" id="outline-container-orgbbe6933">
<h3 id="orgbbe6933">Get by Label</h3>
<div class="outline-text-3" id="text-orgbbe6933">
<p>
kubectl describe deployment能够看到其中的label，
根据label进行筛选：
</p>
<pre class="example">
kubectl get pods -l run=nginx 
kubectl get services -l run=nginx
</pre>
</div>
</div>
<div class="outline-3" id="outline-container-org4028a9d">
<h3 id="org4028a9d">Apply New Label</h3>
<div class="outline-text-3" id="text-org4028a9d">
<p>
kubectl label pod $POD_NAME app=v1
</p>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org4dbacbc">
<h2 id="org4dbacbc">Namespaces Operations</h2>
<div class="outline-text-2" id="text-org4dbacbc">
<p>
kubectl可以通过–namespace或者-n选项指定namespace。如果不指定，默认为default。
通过设置–all-namespace=true来查看所有namespace下的资源。
</p>
<p>
通过 {% raw %} kubectl config set-context &lt;context_name&gt; --namespace=&lt;namespace&gt; {% endraw %} 设置context的默认namespce
</p>
</div>
<div class="outline-3" id="outline-container-org1aac8e7">
<h3 id="org1aac8e7">Get Namespaces :: kubectl get namespaces</h3>
<div class="outline-text-3" id="text-org1aac8e7">
<div class="org-src-container">
<pre class="src src-sh">$ kubectl get namespaces
<span style="font-weight: bold; font-style: italic;">## </span><span style="font-weight: bold; font-style: italic;">OUTPUT</span>
NAME          STATUS   AGE
default       Active   19d
kube-public   Active   19d
kube-system   Active   19d
sre           Active   9d
</pre>
</div>
<p>
STATUS包含两种状态：Active, Terminated（在namspace删除的过程中）
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org8d0ab64">
<h3 id="org8d0ab64">Create Namespace ::</h3>
<div class="outline-text-3" id="text-org8d0ab64">
<ul class="org-ul">
<li>Create using command line: {% raw %} kubectl create namespace &lt;name&gt; {% endraw %}</li>
<li>Create from file: {% raw %} kubectl create -f &lt;file_path&gt; {% endraw %}</li>
</ul>
<div class="org-src-container">
<pre class="src src-ymal">apiVersion: v1
kind: Namespace
metadata:
  name: new-namespace
</pre>
</div>
<div class="org-src-container">
<pre class="src src-sh">$ kubectl create -f ./my-namespace.yaml
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org4891c9a">
<h3 id="org4891c9a">Delete Namespace :: kubectl delete namespaces &lt;name&gt;</h3>
<div class="outline-text-3" id="text-org4891c9a">
<p>
Attentions:
</p>
<ul class="org-ul">
<li>删除Namespace会删除所有属于该Namespace下的资源</li>
<li>Can NOT delete 'default' and 'kube-system'</li>
</ul>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
